using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using YYJ.Entities;

namespace YYJ.UI {
    public partial class SelectBoardForm : BaseForm {
        public SelectBoardForm() {
            InitializeComponent();
        }

        public void init() {
            loadTreeView();
            btnOK.Enabled = false;
        }

        private void loadTreeView() {
            trv.Nodes.Clear();
            IList<Equipment> eqs = Equipment.getAll();
            foreach( Equipment eq in eqs ) {
                TreeNode eqNode = new TreeNode( eq.Name );
                eqNode.Tag = eq;
                IList<Car> cars = Car.getAllByEquipmentId( eq.ID );
                foreach( Car car in cars ) {
                    TreeNode carNode = new TreeNode( car.Name );
                    carNode.Tag = car;
                    eqNode.Nodes.Add( carNode );
                }
                trv.Nodes.Add( eqNode );
            }
        }

        internal Board SelectedBoard {
            get { return _board; }
        }
        private Board _board = null;

        private void btnOK_Click( object sender, EventArgs e ) {
            this._board = trv.SelectedNode.Tag as Board;
            this.DialogResult = DialogResult.OK;
            //this.Hide();
        }

        private void btnCancel_Click( object sender, EventArgs e ) {
            this.DialogResult = DialogResult.Cancel;
        }

        private void trv_NodeMouseDoubleClick( object sender, TreeNodeMouseClickEventArgs e ) {
            if( !( e.Node.Tag is Board ) ) return;
            this._board = e.Node.Tag as Board;
            this.DialogResult = DialogResult.OK;
        }

        private void trv_BeforeExpand( object sender, TreeViewCancelEventArgs e ) {
            TreeNode node = e.Node;

            // load the grand sons of this node
            foreach( TreeNode child in node.Nodes ) {
                if( child.Nodes != null && child.Nodes.Count > 0 ) // children has loaded
                    continue;

                object objTag = child.Tag;
                if( objTag is Car ) {
                    IList<Machine> machines = Machine.getAllByCarId( ( objTag as Car ).ID );
                    foreach( Machine machine in machines ) {
                        TreeNode grandson = new TreeNode( machine.Name );
                        grandson.Tag = machine;
                        child.Nodes.Add( grandson );
                    }
                } else if( objTag is Machine ) {
                    IList<Board> bs = Board.getAllByMachineId( ( objTag as Machine ).ID );
                    foreach( Board b in bs ) {
                        TreeNode grandson = new TreeNode( b.Name );
                        grandson.Tag = b;
                        child.Nodes.Add( grandson );
                    }
                }
            }
        }

        private void trv_NodeMouseClick( object sender, TreeNodeMouseClickEventArgs e ) {
            btnOK.Enabled = e.Node.Tag is Board;
        }
    }
}